package drds.binlog.binlog_task;

import drds.binlog.common.Authors;
import drds.binlog.common.StorageBatchMode;
import drds.common.$;
import drds.common.Author;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

/**
 * 运行相关参数
 */
@Author(name = Authors.LI_YANG)
public class Parameter
{

    @Setter
    @Getter
    List<SalveInfo> salveInfoList = new ArrayList<SalveInfo>();
    @Setter
    @Getter
    private Long id;
    // storage存储
    @Setter
    @Getter
    private Integer transactionSize = 1024;                      // 支持处理的transaction事务大小
    @Setter
    @Getter
    private StorageBatchMode storageBatchMode = StorageBatchMode.memory_size;         // 基于大小返回结果
    @Setter
    @Getter
    private Integer memoryStorageBufferSize = 16 * 1024;                 // 内存存储的buffer大小
    @Setter
    @Getter
    private Integer memoryStorageBufferMemUnit = 1024;                      // 内存存储的buffer内存占用单位，默认为1kb

    @Setter
    @Getter
    private StorageScavengeMode storageScavengeMode = StorageScavengeMode.ON_ACK;
    @Setter
    @Getter
    private String scavengeSchdule;                                                // 调度规则


    @Setter
    @Getter
    private Integer defaultConnectionTimeoutInSeconds = 30;                        // sotimeout
    @Setter
    @Getter
    private Integer receiveBufferSize = 64 * 1024;
    @Setter
    @Getter
    private Integer sendBufferSize = 64 * 1024;
    // 编码信息
    @Setter
    @Getter
    private Byte connectionCharsetNumber = (byte) 33;
    @Setter
    @Getter
    private String connectionCharset = "UTF-8";


    @Setter
    @Getter
    private List<String> positions = new ArrayList<String>();
    ;                                                      // 数据库positions信息


    @Setter
    @Getter
    private Integer fallbackIntervalInSeconds = 60;                        // 数据库发生切换查找时回退的时间

    // 心跳检查信息
    @Setter
    @Getter
    private Boolean detectingEnable = true;                      // 是否开启心跳语句
    @Setter
    @Getter
    private Boolean heartbeatHaEnable = false;                     // 是否开启基于心跳检查的ha功能
    @Setter
    @Getter
    private String detectingSQL;                                                   // 心跳sql
    @Setter
    @Getter
    private Integer detectingIntervalInSeconds = 3;                         // 检测频率
    @Setter
    @Getter
    private Integer detectingTimeoutThresholdInSeconds = 30;                        // 心跳超时时间
    @Setter
    @Getter
    private Integer detectingRetryTimes = 3;                         // 心跳检查重试次数


    @Setter
    @Getter
    private String app;
    @Setter
    @Getter
    private String group;
    // media配置信息
    @Setter
    @Getter
    private String mediaGroup;
    // metaq 存储配置信息
    @Setter
    @Getter
    private String metaqStoreUri;

    // ddl同步支持，隔离dml/ddl
    @Setter
    @Getter
    private Boolean ddlIsolation = Boolean.FALSE;             // 是否将ddl单条返回
    @Setter
    @Getter
    private Boolean filterTableError = Boolean.FALSE;             // 是否忽略表解析异常

    @Setter
    @Getter
    private String schemaNameAndTableNameRegexFilter = null;

    @Setter
    @Getter
    private String blackSchemaNameAndTableNameRegexFilter = null;                      // 匹配黑名单,忽略解析


    @Setter
    @Getter
    private String tableMetaDataManagerJdbcUrl;
    @Setter
    @Getter
    private String tableMetaDataManagerJdbcUserName;
    @Setter
    @Getter
    private String tableMetaDataManagerJdbcPassword;


    public Integer getTransactionSize()
    {
        return transactionSize != null ? transactionSize : 1024;
    }


    public List<String> getPositions()
    {
        for (SalveInfo salveInfo : salveInfoList)
        {
            positions.add(buildPosition(salveInfo.getLogFileName(), salveInfo.getLogFileOffest(), salveInfo.getTimestamp()));
        }
        return positions;
    }


    private String buildPosition(String logFileName, Long logFileOffest, Long timestamp)
    {
        StringBuilder masterBuilder = new StringBuilder();
        if ($.isNotNullAndNotEmpty(logFileName) || logFileOffest != null || timestamp != null)
        {
            masterBuilder.append('{');
            if ($.isNotNullAndNotEmpty(logFileName))
            {
                masterBuilder.append("\"journalName\":\"").append(logFileName).append("\"");
            }

            if (logFileOffest != null)
            {
                if (masterBuilder.length() > 1)
                {
                    masterBuilder.append(",");
                }
                masterBuilder.append("\"setIndexAsOriginIndexAndOffset$originIndex\":").append(logFileOffest);
            }

            if (timestamp != null)
            {
                if (masterBuilder.length() > 1)
                {
                    masterBuilder.append(",");
                }
                masterBuilder.append("\"timestamp\":").append(timestamp);
            }
            masterBuilder.append('}');
            return masterBuilder.toString();
        } else
        {
            return null;
        }
    }


    public Boolean getDdlIsolation()
    {
        return ddlIsolation == null ? false : ddlIsolation;
    }


    public Boolean getFilterTableError()
    {
        return filterTableError == null ? false : filterTableError;
    }


}
